<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsId">
        <el-select v-model="queryParams.goodsId" clearable filterable placeholder="请选择">
            <el-option
              v-for="item in goodsList"
              :key="item.goodsId"
              :label="item.goodsName"
              :value="item.goodsId">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="收件人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入收件人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.goods_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
           <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商" prop="tenantId" v-if="isPla">
        <el-select v-model="queryParams.tenantId" placeholder="请选择供应商" clearable size="small">
           <el-option
            v-for="item in tenantList"
            :key="item.tenantId"
            :label="item.name"
            :value="item.tenantId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['biz:orderInfo:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:orderInfo:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:orderInfo:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['biz:orderInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderInfoList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="订单ID" align="center" prop="orderId" v-show="false"/>
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="商品缩略图" align="center" prop="goodsImg">
        <template slot-scope="scope">
          <el-image v-if="scope.row.goodsImg!=null" style="width:70px; height:70px" :src="scope.row.goodsImg"></el-image>
         </template>
      </el-table-column>
      <!-- <el-table-column label="规格" align="center" prop="specs" /> -->
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="数量" align="center" prop="num" />
      <el-table-column label="兑换码" align="center" prop="redeemCode" />
      <el-table-column label="收件人名称" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="商品类型" align="center" prop="type">
        <template slot-scope="scope">
           <dict-tag :options="dict.type.goods_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="供应商" align="center" prop="tenantName" />
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="下单时间" align="center" prop="orderTime" />
      <el-table-column label="发货时间" align="center" prop="deliveryTime" />
      <el-table-column label="地区" align="center" prop="region" />
      <el-table-column label="详细地址" align="center" prop="address" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-truck"
            @click="handleSend(scope.row)"
            v-hasPermi="['biz:orderInfo:send']"
            v-show="scope.row.status==0"
          >发货</el-button>
           <el-button
            size="mini"
            type="text"
            icon="el-icon-bicycle"
            @click="handleExpress(scope.row)"
            v-show="(scope.row.status==1 || scope.row.status==2) && scope.row.type == 0"
          >物流</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['biz:orderInfo:edit']"
            v-show="scope.row.status==0"
          >修改</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:orderInfo:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单Id" prop="orderNo" v-show="false">
          <el-input v-model="form.orderId" placeholder="请输入订单编号" :disabled="true"/>
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" :disabled="true"/>
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" :disabled="true"/>
        </el-form-item>
        <!-- <el-form-item label="商品缩略图" prop="goodsImg">
          <el-input v-model="form.goodsImg" placeholder="请输入商品缩略图" />
        </el-form-item>
        <el-form-item label="规格" prop="specs">
          <el-input v-model="form.specs" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入数量" />
        </el-form-item> -->
        <el-form-item label="收件人" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入收件人名称" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="地区" prop="region">
          <!-- <el-input v-model="form.region" placeholder="请输入地区" /> -->
          <el-cascader
            v-model="areaList"
            :options="options"
            :props="{ expandTrigger: 'hover' }"
            @change="handleChange"></el-cascader>
        </el-form-item>
        <!-- <el-form-item label="类型【0实物订单、1虚拟订单】" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型【0实物订单、1虚拟订单】">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item> -->
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态" clearable size="small">
            <el-option label="待发货" :value="0" />
            <el-option label="待收货" :value="1" />
            <el-option label="已收货" :value="2" />
            <el-option label="已关闭" :value="3" />
            <el-option label="已兑换" :value="4" />
            <el-option label="已取消" :value="-1" />
            <el-option label="未兑换" :value="-2" />
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 订单发货 -->
    <el-dialog :title="title" :visible.sync="send" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单Id" prop="orderNo" v-show="false">
          <el-input v-model="form.orderId" placeholder="请输入订单编号" :disabled="true"/>
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" :disabled="true"/>
        </el-form-item>
        <el-form-item label="快递公司" prop="companyCode">
          <el-select v-model="form.companyCode" filterable clearable placeholder="请选择快递公司">
             <el-option
              v-for="item in expressCompanyList"
              :key="item.companyCode"
              :label="item.companyName"
              :value="item.companyCode">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="快递单号" prop="trackingNo">
          <el-input v-model="form.trackingNo" placeholder="请输入物流单号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSendForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 订单发货 -->
    <el-dialog title="物流信息" :visible.sync="isExpress" width="800px" append-to-body>
      <dev style="height: 300px;">
        <el-steps direction="vertical" :active="1">
          <el-step :title="item.ftime" v-for="(item,index) in expressList" :key = "index" :description="item.context"></el-step>
        </el-steps>
      </dev>
    </el-dialog>
  </div>
</template>


<script>
import { listOrderInfo, getOrderInfo, delOrderInfo, addOrderInfo, updateOrderInfo, exportOrderInfo, send, getExpressByOrderId } from "@/api/biz/orderInfo/orderInfo";
import { isPlatform, listNotPage } from "@/api/system/tenant"
import { getExpressCompanyList } from "@/api/biz/expressCompany/expressCompany";
import { goodslistNotPage } from "@/api/biz/goodsInfo/goodsInfo";
import area from '@/utils/area.json';
export default {
  name: "OrderInfo",
  dicts: ['goods_type','order_status'],
  data() {
    return {
      baseUrl: "",
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单信息表格数据
      orderInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      send: false,
      isExpress: false,
      expressList: [],
      // 租户列表
      tenantList: [],
      // 商品列表
      goodsList: [],
      isPla: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        goodsName: null,
        specs: null,
        price: null,
        num: null,
        userName: null,
        phone: null,
        region: null,
        type: null,
        status: null,
        address: null,
        tenantId: null,
        goodsId: null
      },
      areaList: [],
      // 表单参数
      form: {},
      //快递公司下拉列表
      expressCompanyList: [],
      options: area,
      // 表单校验
      rules: {
        goodsName:[
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        userName:[
          { required: true, message: "收货人不能为空", trigger: "blur" }
        ],
        phone:[
          { required: true, message: "收货人手机号码不能为空", trigger: "blur" }
        ],
        region:[
          { required: true, message: "收货地区不能为空", trigger: "blur" }
        ],
        status:[
          { required: true, message: "订单状态不能为空", trigger: "blur" }
        ],
        address:[
          { required: true, message: "详细地址不能为空", trigger: "blur" }
        ],
        companyCode:[
          { required: true, message: "快递公司不能为空", trigger: "blur" }
        ],
        trackingNo:[
          { required: true, message: "快递单号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getIsPlatform();
    this.getTenantList();
    this.getExpressCompanyList();
    this.getGoodsList()
  },
  methods: {
    /** 查询订单信息列表 */
    getList() {
      this.loading = true;
      listOrderInfo(this.queryParams).then(response => {
        this.orderInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.send = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderNo: null,
        goodsId: null,
        goodsName: null,
        goodsImg: null,
        specs: null,
        price: null,
        num: null,
        userName: null,
        phone: null,
        region: null,
        type: null,
        status: null,
        address: null,
        tenantId: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        companyCode: null,
        trackingNo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrderInfo(orderId).then(response => {
        this.form = response.data;
        if(response.data.area!=null){
          this.areaList = response.data.area.split(",")
        }
        this.open = true;
        this.title = "修改订单信息";
      });
    },
    handleSend(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrderInfo(orderId).then(response => {
        this.form = response.data;
        this.send = true;
        this.title = "订单发货";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            this.form.area = this.areaList.join(',');
            this.form.region = this.areaList.join('');
            updateOrderInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrderInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除订单信息编号为"' + orderIds + '"的数据项？').then(function() {
        return delOrderInfo(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有订单信息数据项？').then(() => {
        this.exportLoading = true;
        return exportOrderInfo(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    //租户列表
    getTenantList(){
      listNotPage().then(res => {
        if(res.code==200){
          this.tenantList=res.data;
        }
      })
    },
    //是否是平台
    getIsPlatform(){
      isPlatform().then(res => {
       if(res.code==200){
          this.isPla=res.data;
        }
      })
    },
    //快递公司列表
    getExpressCompanyList(){
      getExpressCompanyList().then(res => {
       if(res.code==200){
          this.expressCompanyList=res.data;
        }
      })
    },
    //订单发货
    submitSendForm(){
      let data={
        "orderId": this.form.orderId,
        "companyCode": this.form.companyCode,
        "trackingNo": this.form.trackingNo
      }
      send(data).then(res=>{
          if(res.code==200){
            this.send=false;
            this.getList();
          }else{
            this.$message.error(res.data.msg);
          }
      })
    },
    handleExpress(row){
      const orderId = row.orderId
      getExpressByOrderId(orderId).then(res => {
        this.expressList = res.data;
        this.isExpress = true;
      });
    },
    //商品列表
    getGoodsList(){
      goodslistNotPage().then(res => {
        this.goodsList = res.data;
      })
    },
    handleChange(value) {
      console.log(value);
    }
  }
};
</script>
